package com.ayh.campusfun.vo;

import cn.hutool.core.bean.BeanUtil;
import com.ayh.campusfun.entity.FunComment;
import com.ayh.campusfun.entity.LoveComment;
import com.ayh.campusfun.entity.TopicComment;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author ayh
 * @create 2021/3/26 13:50
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommentVO <T>{

    private Long id;
    private String uid;
    private String nickName;
    private String avatarUrl;
    private Integer commentLevel;
    private Long parentId;
    private Long shareId;
    /**
     * 发表时间
     */
    private Date sendTime;
    /**
     * 评论详情
     */
    private String commentContent;

    private List<T> childList;

    public static List<CommentVO<TopicComment>> buildTopicCommentList(List<TopicComment> topicComments){
        Stream<TopicComment> commentStream = topicComments.stream();
        return commentStream.filter(v-> v.getCommentLevel()==1)
                .map(v->{
                    CommentVO<TopicComment> commentVO = new CommentVO<TopicComment>();
                    BeanUtil.copyProperties(v,commentVO);
                    commentVO.setShareId(v.getTopicId());
                    List<TopicComment> topicCommentList = new ArrayList<>();
                    topicComments.forEach(item ->{
                        if(item.getParentId()==v.getId()) topicCommentList.add(item);
                    });
                    commentVO.setChildList(topicCommentList);
                    return commentVO;
                }).collect(Collectors.toList());

    }

    public static List<CommentVO<FunComment>> buildFunCommentList(List<FunComment> comments){
        Stream<FunComment> commentStream = comments.stream();
        return commentStream.filter(v-> v.getCommentLevel()==1)
                .map(v->{
                    CommentVO<FunComment> commentVO = new CommentVO<FunComment>();
                    BeanUtil.copyProperties(v,commentVO);
                    commentVO.setShareId(v.getFunId());
                    List<FunComment> funCommentList = new ArrayList<>();
                    comments.forEach(item ->{
                        if(item.getParentId()==v.getId()) funCommentList.add(item);
                    });
                    commentVO.setChildList(funCommentList);
                    return commentVO;
                }).collect(Collectors.toList());

    }

    public static List<CommentVO<LoveComment>> buildLoveCommentList(List<LoveComment> comments){
        Stream<LoveComment> commentStream = comments.stream();
        return commentStream.filter(v-> v.getCommentLevel()==1)
                .map(v->{
                    CommentVO<LoveComment> commentVO = new CommentVO<LoveComment>();
                    BeanUtil.copyProperties(v,commentVO);
                    commentVO.setShareId(v.getLoveId());
                    List<LoveComment> loveCommentList = new ArrayList<>();
                    comments.forEach(item ->{
                        if(item.getParentId()==v.getId()) loveCommentList.add(item);
                    });
                    commentVO.setChildList(loveCommentList);
                    return commentVO;
                }).collect(Collectors.toList());

    }
}
